﻿using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using System.Linq;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;
using eCommMongo.Core;
using eCommMongo.Core.Data;

namespace eCommMongo.Data
{
    public class MongoRepository<T> : IMongoRepository<T> where T : BaseMongoEntity
    {
        private MongoDatabase database;

        public MongoRepository()
        {
            var connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            database = server.GetDatabase("yiCommerce");
        }

        public T GetById( string id)
        {
            var entityName = Pluralize(typeof(T).Name);
            var query = Query<T>.EQ(e => e.Id, id);
            var collection = database.GetCollection<T>(entityName);
           
            var entity = collection.FindOne(query);
            return entity;
        }

        public string Insert(T entity)
        {
           var entityName =  Pluralize(typeof(T).Name);

            var collection = database.GetCollection<T>(entityName);
            entity.Id = ObjectId.GenerateNewId().ToString();
            collection.Insert(entity);
            var id = entity.Id;
            return id;
        }

        private static string Pluralize(string word)
        {
            var service = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
            var pluralizedWord = service.Pluralize(word);
            return pluralizedWord;
        }

        public void Update( string id, IMongoUpdate update)
        {
            var entityName = Pluralize(typeof(T).Name);
            var collection = database.GetCollection<T>(entityName);
            var query = Query<T>.EQ(e => e.Id, id);
            collection.Update(query, update);

    
        }

        public void Update( IMongoQuery query, IMongoUpdate update)
        {
            var entityName = Pluralize(typeof(T).Name);
            var collection = database.GetCollection<T>(entityName);
            collection.Update(query, update);
           
        }

        public void Save(T entity)
        {
            var entityName = Pluralize(typeof(T).Name);
            var collection = database.GetCollection<T>(entityName);
            collection.Save(entity);
        }

        public void Delete(string id)
        {
            var entityName = Pluralize(typeof(T).Name);
            var collection = database.GetCollection<T>(entityName);
            var query = Query<T>.EQ(e => e.Id, id);
            collection.Remove(query);
        }

        public IQueryable<T> Table()
        {
            var entityName = Pluralize(typeof(T).Name);
            return database.GetCollection<T>(entityName).AsQueryable<T>();
        }


        public IQueryable<T> Table(IMongoQuery query)
        {
            var entityName = Pluralize(typeof(T).Name);
            return database.GetCollection<T>(entityName).Find(query).AsQueryable<T>();
        }
    }
}
